<template>
  <div class="sidebar g-menu-texture" :class="{'collapse': collapse}">
    <div class="sidebar-logo-container" :class="{'collapse': collapse}">
      <h1 class="sidebar-title" >侧边栏标题</h1>
      <div class="btn-collapse" @click="collapseChage" >
        <i v-if="!collapse" class="el-icon-s-fold"></i>
        <i v-else class="el-icon-s-unfold"></i>
      </div>
    </div>
    <el-menu class="sidebar-el-menu" :default-active="onRoutes" router > <!--:collapse="collapse" -->
      <siderbarItem :item="items" @change="hoverChange" ></siderbarItem>
    </el-menu>
  </div>
</template>
<script>

import bus from '../common/bus';
import siderbarItem from './SidebarItem'
export default {
  props:{
    items:{
      type:Array,
      default:()=>{return []}
    }
  },
  data() {
    return {
      collapse: false,
      headBanner:false,
      horizontal:'',
      isClosed:false,
        openMenu:[]
    }
  },
  components:{
    siderbarItem
  },
  methods:{
    handleShow() {
      generalConfig.hasTopLayout === undefined ? this.headBanner = true : this.headBanner = generalConfig.hasTopLayout;
      if(this.headBanner){
          this.horizontal='horizontal'
      }
    },
    collapseChage() {
      this.isClosed=!this.isClosed;
      this.collapse = !this.collapse;
      bus.$emit('collapse', this.collapse);
      console.log(this.openMenu)
    },
    hoverChange(){
      if(this.isClosed){
          this.collapse = !this.collapse;
          bus.$emit('collapse', this.collapse);
      }
    }
  },
  computed: {
    onRoutes() {
      // 已改写成只需匹配到一级路由即可匹配菜单
      return this.$route.matched[1].path
    },

  },
  created() {
    this.handleShow()
    // 通过 Event Bus 进行组件间通信，来折叠侧边栏
    bus.$on('collapse', msg => {
      this.collapse = msg;
    })
  }
}
</script>
<style lang="scss" scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 50px;
  bottom: 0;
  overflow-y: auto;
  overflow-x: scroll;
  z-index: 6;
  border-right: 1px solid #d0d0d0;
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
}
.sidebar>ul {
  height: calc(100% - 48px);
  overflow: auto;
  margin:0 10px 0  -10px ;
  &::-webkit-scrollbar {
    width: 0;
  }
}
.sidebar-logo-container {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 48px;
  line-height: 48px;
  overflow: hidden;
  border-bottom: 1px solid #f0f0f0;
  box-sizing: border-box;
  .sidebar-title {
    display: inline-block;
    color: #262626;
    font-weight: 600;
    font-size: 16px;
    margin: 0 0 0 16px;
    line-height: 48px;
  }
  .btn-collapse {
    font-size: 20px;
    text-align: center;
    width: 48px;
    cursor: pointer;
  }
}
.collapse {
  .sidebar-title {
    display: none;
  }
  .btn-collapse {
    width: 100%;
  }
}
/deep/ .el-menu-item,
.el-submenu__title {
  height: 50px;
  line-height: 50px
}
/deep/ .el-menu {
  border-right: none;
}
@media (max-width: 1400px) {
  .sidebar-el-menu:not(.el-menu--collapse) {
    width: 200px;
  }
}

</style>